package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.WemediaConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult findList() {
        return ResponseResult.okResult(list());
    }

    /**
     * 保存频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        if (wmChannel == null || StringUtils.isBlank(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 判断频道名称否重复
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery()
                .eq(WmChannel::getName, wmChannel.getName()));
        if (channel != null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已存在");
        //保存频道
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询频道
     *
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        //檢查參數
        if (channelDto == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //检测分页参数
        channelDto.checkParam();
        //分页查询 todo
        Page<WmChannel> wmChannelPage = new Page<>(channelDto.getPage(), channelDto.getSize());
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.<WmChannel>lambdaQuery();
        if (StringUtils.isNotBlank(channelDto.getName()))
            wrapper.like(WmChannel::getName, channelDto.getName());

        Page<WmChannel> page = page(wmChannelPage, wrapper);

        ResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        if (id == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        WmChannel wmChannel = getById(id);
        if (wmChannel == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        if (wmChannel.getStatus())
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已启用，不可删除");
//检查是否被引用
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id));
        if (count > 0)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道被引用，不可删除");
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        if (wmChannel == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //是否被引用
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery()
                .eq(WmNews::getChannelId, wmChannel.getId())
                .eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode()));
        if(count > 0)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道被引用，不可修改");

        wmChannel.setCreatedTime(new Date());
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
